package com.qxglxt.qxgl.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.qxglxt.qxgl.dao.IUsersDao;
import com.qxglxt.qxgl.dto.UsersDTO;
import com.qxglxt.qxgl.model.Users;
import com.qxglxt.qxgl.service.IUsersService;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@ApiOperation("用户列表")
@RestController
@RequestMapping("/users")
public class UsersController {
    @Resource
    private IUsersService usersService;
    @Resource
    private IUsersDao usersDao;

    @ApiOperation(value = "查询所有用户", notes = "查询所有用户")
    @GetMapping
    public Map<String, Object> findAll() {
        List<Users> list = usersDao.findAll();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "成功");
        map.put("count", usersDao.count());
        map.put("data", list);
        return map;
    }

    @ApiOperation(value = "多条件分页查询用户",notes = "多条件分页查询用户")
    @PostMapping(value = "findPage")
    public Map<String, Object> findPage(UsersDTO dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Users> page = usersService.findPage(dto);
        map.put("code", 0);
        map.put("msg", "查询出功");
        map.put("count", page.getTotalElements());
        map.put("data", page.getContent());
        return map;
    }

    @ApiOperation(value = "更新用户信息", notes = "添加或修改")
    @PostMapping("/addOrEdit")
    public Users saveMember(@RequestBody Users u) {
        return usersService.saveUsers(u);
    }

    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @ApiImplicitParam(name = "id", value = "编号", required = true, paramType = "path")
    @DeleteMapping(value = "/id")
    public void del(@PathVariable Integer id) {
        usersService.del(id);

    }

    @ApiOperation(value = "重置密码", notes = "重置密码")
    @ApiImplicitParam(name = "id", value = "用户编号", required = true, paramType = "path")
    @PutMapping(value = "/{id}")
    public int pwdReset(@PathVariable Integer id) {
        return usersDao.pwdReset(id);
    }

    @ApiOperation(value = "锁定用户", notes = "锁定用户")
    @ApiImplicitParam(name = "id", value = "用户编号", required = true, paramType = "path")
    @PutMapping("/lockoutUser/{id}")
    public int lockoutUser(@PathVariable Integer id) {
        return usersDao.lockoutUser(id);
    }

    @ApiOperation(value = "解锁用户", notes = "解锁用户")
    @ApiImplicitParam(name = "id", value = "用户编号", required = true, paramType = "path")
    @PutMapping("/notLockoutUser/{id}")
    public int notLockoutUser(@PathVariable Integer id) {
        return usersDao.notLockoutUser(id);
    }
}